﻿using Books.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Books.SQLite
{
    public class SQLManagerment : ISQLManagerment
    {
        private readonly SQLiteAsyncConnection dbConnection;
        public SQLManagerment(string databasePath)
        {

            dbConnection = new SQLiteAsyncConnection(databasePath);
        }
        public async void Initialize()
        {
            if (!ExistsTable<FileDownLoadComplete>())
            {
                await dbConnection.CreateTableAsync<FileDownLoadComplete>();
            }
        }
        public SQLiteAsyncConnection GetAsyncConnection()
        {
            throw new NotImplementedException();
        }
        private bool ExistsTable<T>()
        {

            using (var conn = new SQLiteConnection(App.DatabasePath))
            {
                var command = new SQLiteCommand(conn)
                {
                    CommandText =
                        "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='" +
                        typeof(T).Name + "'"
                };
                return (command.ExecuteScalar<int>() > 0);
            }

        }

    }
}
